package org.sbbs.app.license.webapp.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.sbbs.app.license.model.VenderInfo;
import org.sbbs.app.license.service.VenderInfoManager;
import org.sbbs.base.webapp.action.BaseMaintainAction;

import com.googlecode.genericdao.search.Search;

public class VenderInfoMaintainAction
    extends BaseMaintainAction<VenderInfo, Long> {
    /**
     * 
     */
    private static final long serialVersionUID = -3927165634035917226L;

    private VenderInfo model;

    private VenderInfoManager venderInfoManager;

    public VenderInfoManager getVenderInfoManager() {
        return venderInfoManager;
    }

    public void setVenderInfoManager( VenderInfoManager venderInfoManager ) {
        this.venderInfoManager = venderInfoManager;
    }

    public VenderInfo getModel() {
        return model;
    }

    public void setModel( VenderInfo model ) {
        this.model = model;
    }

    public String edit() {

        int vc = this.venderInfoManager.count( new Search() );
        if ( vc > 0 ) {
            List<VenderInfo> vl = this.venderInfoManager.findAll();
            this.setModel( vl.get( 0 ) );
            this.setEditTypeEdit();
        }
        else {
            this.setModel( new VenderInfo() );
            this.setEditTypeAdd();
        }

        return SUCCESS;
    }

    public String save() {
        try {
            HttpServletRequest req = this.getRequest();
            // boolean isNew = ( this.getEditType() == 1 );
            this.venderInfoManager.save( this.getModel() );
            this.setAjaxMessage( getText( ( isNew() ) ? "venderInfo.added" : "venderInfo.updated", "no msg key found,save successed." ) );
            this.setAjaxStatus( AJAX_STATUS_SUCCESS );
        }
        catch ( Exception e ) {
            this.setAjaxMessage( getText( "error.saved", new String[] { e.getMessage() } ) );
            this.setAjaxStatus( AJAX_STATUS_ERROR );
        }
        return SUCCESS;

    }

}
